<template>
  <div class="lb-system-payment">
    <top-nav></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="120px"
      >
        <el-form-item label="商户号" prop="mch_id">
          <el-input
            v-model="subForm.mch_id"
            placeholder="请输入商户号"
          ></el-input>
        </el-form-item>
        <el-form-item label="支付秘钥" prop="pay_key">
          <el-input
            v-model="subForm.pay_key"
            placeholder="请输入支付秘钥"
          ></el-input>
        </el-form-item>
        <el-form-item label="cert_path" prop="cert_path">
          <div class="lb-file-input flex-between">
            <div :class="[{ 'c-title': subForm.cert_path }]">
              {{ subForm.cert_path | handleFileName }}
            </div>
            <lb-cover
              type="button"
              fileType="cert"
              :fileSize="1"
              @selectedFiles="getFile($event, 'cert_path')"
            ></lb-cover>
          </div>
        </el-form-item>
        <el-form-item label="key_path" prop="key_path">
          <div class="lb-file-input flex-between">
            <div :class="[{ 'c-title': subForm.key_path }]">
              {{ subForm.key_path | handleFileName }}
            </div>
            <lb-cover
              type="button"
              fileType="cert"
              :fileSize="1"
              @selectedFiles="getFile($event, 'key_path')"
            ></lb-cover>
          </div>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitForm" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    let checkPayKey = (rule, value, callback) => {
      let reg = /^[0-9a-zA-Z]*$/g
      if (!reg.test(value)) {
        callback(new Error('请输入字母数字类型'))
      } else {
        callback()
      }
    }
    return {
      fileName: '',
      subForm: {
        mch_id: '',
        pay_key: '',
        cert_path: '',
        key_path: ''
      },
      subFormRules: {
        mch_id: {
          required: true,
          type: 'string',
          message: '请输入商户号',
          trigger: 'blur'
        },
        pay_key: [
          {
            required: true,
            type: 'string',
            message: '请输入支付秘钥',
            trigger: 'blur'
          },
          { validator: checkPayKey, trigger: 'blur' }
        ],
        cert_path: {
          required: true,
          type: 'string',
          message: '请上传证书',
          trigger: 'change'
        },
        key_path: {
          required: true,
          type: 'string',
          message: '请上传证书',
          trigger: 'change'
        }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  methods: {
    async getFormInfo () {
      let { code, data } = await this.$api.system.payConfigInfo()
      if (code !== 200) return
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getFile (file, key) {
      let len = file.length - 1
      this.subForm[key] = file[len].url
    },
    submitForm () {
      this.$refs['subForm'].validate(valid => {
        if (valid) {
          let { subForm } = this
          this.$api.system.payConfigUpdate(subForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
            }
          })
        }
      })
    }
  },
  watch: {
    'subForm.cert_path' (newValue, oldValue) {
      if (newValue && !oldValue) {
        this.$refs['subForm'].clearValidate('cert_path')
      }
    },
    'subForm.key_path' (newValue, oldValue) {
      if (newValue && !oldValue) {
        this.$refs['subForm'].clearValidate('key_path')
      }
    }
  },
  filters: {
    handleFileName (url) {
      let text = '请上传文件'
      if (url) {
        text = url.substring(url.lastIndexOf('/') + 1)
      }
      return text
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-payment {
  width: 100%;
  .el-input {
    width: 300px;
  }
}
</style>
